package com.cqc.transition01;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by ${cqc} on 2017/7/21.
 * viewSwitcher：切换view，子view最多有2个，超过2个会报错
 * Can't add more than 2 views to a ViewSwitcher
 */

public class ViewSwitcherActivity extends AppCompatActivity {

    @BindView(R.id.ViewSwitcher)
    ViewSwitcher viewSwitcher;
    @BindView(R.id.btn1)
    Button btn1;
    @BindView(R.id.btn2)
    Button btn2;

    private Context context = this;
    private TextView tv;
    private ImageView iv;
    private int count=0;


    private List<View> viewList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_view_switcher);
        ButterKnife.bind(this);

        createViews();
        addViews();

        Animation slide_in = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left);
        Animation slide_out = AnimationUtils.loadAnimation(context, android.R.anim.slide_out_right);
        viewSwitcher.setInAnimation(slide_in);
        viewSwitcher.setOutAnimation(slide_out);

        viewSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
            @Override
            public View makeView() {
                return viewList.get((count++)%viewList.size());
            }
        });

        LogUtil.d("onCreate");
    }

    private void addViews() {
        //也可以不使用addView(),而是setFactory(...)获取子view
//        viewSwitcher.addView(text_view);
//        viewSwitcher.addView(iv);

        viewList.add(tv);
        viewList.add(iv);
    }

    private void createViews() {
        tv = new TextView(context);
        tv.setText("Text");
        tv.setGravity(Gravity.CENTER);

        iv = new ImageView(context);
        iv.setImageResource(R.mipmap.ic_launcher);
    }

    @OnClick({R.id.btn1, R.id.btn2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn1:
                viewSwitcher.showPrevious();
                break;
            case R.id.btn2:
                viewSwitcher.showNext();
                break;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        LogUtil.d("onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        LogUtil.d("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        LogUtil.d("onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        LogUtil.d("onStop");
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        LogUtil.d("onDestroy");
    }
}
